/*==========================================================================
 *
 *  Copyright (C) 1995 Microsoft Corporation. All Rights Reserved.
 *
 *  File:       Assert.cpp
 *  Content:    Windows assert handler
 *              You must externally define hwndGame and szAppName for this
 *              to work.
 *
 ***************************************************************************/
#include "stdafx.h"
#include <windows.h>

#include <stdio.h>	// for FILE
#include "Assert.h"

///////////
// display error message and quit at last
// hwnd		:	hangle window to display error
// code		:	error code
// message	:	error message
// filename	:	if the message needs filename, the filename
void ErrorMessage( HWND hwnd, int code, LPCTSTR message, LPCTSTR filename/*=NULL*/)
{
	char mess[256];
	strcpy( mess, "Error(" );
	char tmp[33];
	itoa(code,tmp,10);
	strcat( mess, tmp ); strcat( mess, ")" ); strcat( mess, message );
	if(filename)
	{
		strcat( mess, "<" ); strcat( mess, filename ); strcat( mess, ">" );
	}

	::ShowWindow( hwnd, SW_MINIMIZE );
	::MessageBox(hwnd, mess, "ERROR", MB_OK);
	PostQuitMessage(-1);
}


///////////
// display error message
// hwnd		:	hangle window to display error
// code		:	error code
// message	:	error message
// filename	:	if the message needs filename, the filename
void ErrorMessageNoQuit( HWND hwnd, int code, LPCTSTR message, LPCTSTR filename/*=NULL*/)
{
	char mess[256];
	strcpy( mess, "Error(" );
	char tmp[33];
	itoa(code,tmp,10);
	strcat( mess, tmp ); strcat( mess, ")" ); strcat( mess, message );
	if(filename)
	{
		strcat( mess, "<" ); strcat( mess, filename ); strcat( mess, ">" );
	}
	
	::ShowWindow( hwnd, SW_MINIMIZE );
	::MessageBox(hwnd, mess, "ERROR", MB_OK);
	::ShowWindow( hwnd, SW_MAXIMIZE );
}

// output debug values
// nNum		:	value to output
// pStr		:	devide symbles after the value
// nHex		:	decimal or hex value to display
void OutputString( int nNum, const char *pStr , int nHex)
{
	char tmp[129];
	itoa( nNum, tmp, nHex );
	strcat( tmp, pStr );
	OutputDebugString( tmp );
}

void WriteErrorFile(LPCTSTR filename,WORD str)
{
	FILE * p;

	p=fopen(filename,"at");
	fprintf(p,"%d\n",str);
	fclose(p);

}

void WriteLogFile( LPCTSTR filename, LPCTSTR str )
{
	FILE *fp = fopen( filename, "at" );
	fprintf( fp, "%s", str );
	fclose( fp );
}
///////////

#ifdef _ASSERT_
void AssertFail(const char szErr[], const char szFileName[], int nLine, char szMessage[])
    {
    char szT[256];

    if (szMessage != NULL)
        wsprintf(szT, "Assert(%s);\nFile %s, line %d.  %s", szErr, szFileName, nLine, szMessage);
    else 
        wsprintf(szT, "Assert(%s);\nFile %s, line %d.", szErr, szFileName, nLine);
    switch (MessageBox(GetActiveWindow(), szT, "ASSERT FAILED", MB_ABORTRETRYIGNORE | MB_ICONSTOP | MB_APPLMODAL))
        {
        case IDABORT:
            SendMessage(GetActiveWindow(), WM_CLOSE, 0, 0);
        case IDRETRY:
            _asm int 3;
            // Fall Through //
        case IDIGNORE:
            break;

        } // switch
    } // AssertFail


#endif // ASSERT

